﻿$(function() {
	var $modal = $("#my-modal-loading");
	var load_text = $("#loading_text");
	load_text.text("初始化中...");
	var headPath = localStorage.getItem("head_image");
	$(".img_test1").attr("src", headPath); 
	var timestamp;
	var nonceStr;
	var signature;
	//var url = 'http://yx.fxtourong.com/index.php/V1/Web/dongtai_yuyin.html';
	var url = location.href;
	var recordPath1;
	var recordPath2;
	var recordPath3;
	var recordServerPath1;
	var recordServerPath2;
	var recordServerPath3;
	var audioServerIds = "";
	var countdown = 60;
	var isPlay = 0;
	var query_id = localStorage.getItem("query_id");
	var urlPath = "http://yx.fxtourong.com/V1/Goods/createDynamic" + "?store_id=" + query_id;
	$("#cancel_back").click(function(){
		window.history.back(-1);
	});
	$modal.modal();
	$.ajax({
		type: "POST",
		url: "http://yx.fxtourong.com/V1/Wechat/getSignPackage",
		data: {
			token: localStorage.getItem("token"),
			url: url,
			verify: "yin@xiong8_(fx-tourong"
		},
		dataType: "json",
		success: function(data) {
			$modal.modal('close');
			var code = data.code;
			if(code == "1") {
				
				timestamp = data.data.timestamp;
				nonceStr = data.data.nonceStr;
				signature = data.data.signature;
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: 'wx7f13802caf31f385', // 必填，公众号的唯一标识
					timestamp: timestamp, // 必填，生成签名的时间戳
					nonceStr: nonceStr, // 必填，生成签名的随机串
					signature: signature, // 必填，签名，见附录1
					jsApiList: ['chooseImage', 'uploadImage', 'startRecord', 'stopRecord',
						'onVoiceRecordEnd', 'playVoice', 'pauseVoice',
						'stopVoice', 'onVoicePlayEnd', 'uploadVoice',
						'downloadVoice', 'downloadImage'
					] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				wx.ready(function() {
					console.log("aaaaaaaaaaaaaaaa");
				});
			}
		},
		error: function(e){
			$modal.modal('close');
			alert("初始化失败");
		}
	});

	$("#a_openbtn").click(function() {
		if(isPlay == 0) {
			isPlay = 1;
			wx.startRecord();
			$('head').append('<link rel="stylesheet" href="/public/yx/css/record2.css">');
			settime();
			$("#openbtn").text("录音完成");
		} else {
			isPlay = 0;
			wx.stopRecord({
				success: function(res) {
					if(recordPath1 == null || recordPath1 == undefined || recordPath1 == '') {
						recordPath1 = res.localId;
						wx.uploadVoice({
							localId: recordPath1, // 需要上传的音频的本地ID，由stopRecord接口获得
							isShowProgressTips: 0, // 默认为1，显示进度提示
							success: function(res) {
								recordServerPath1 = res.serverId; // 返回音频的服务器端ID
								countdown = 0;
								$("#openbtn").text("开始录音");
								removejscssfile("/public/yx/css/record2.css", "css");
								postData();
							}
						});
					} 
				}
			});
		}
		$("#cancelbtn").click(function() {
			isPlay = 0;
			wx.stopRecord({
				success: function(res) {
					var localId = res.localId;
				}
			});
			countdown = 0;
			$("#openbtn").text("开始录音");
			removejscssfile("/public/yx/css/record2.css", "css");
		});
	});
	
	function postData(){
		var type = 1;
		load_text.text("发送中...");
		$modal.modal();
		$.ajax({
			type: "POST",
			url: urlPath,
			data: {
				token: localStorage.getItem("token"),
				g_type: type,
				details: '',
				cover: '',
				audio : recordServerPath1
			},
			dataType: "json",
			success: function(data) {
				$modal.modal('close');
				console.log(data);
				var code = data.code;
				console.log(code);
				if(code == "1") {
					new Toast({context:$('body'),message:'发送成功'}).show();
					location.href = "query_detail.html";
				} else {
					alert(data.message + "");
				}
			},
			error: function(e){
				$modal.modal('close');
				alert("服务请求异常！");
			}
		});
	}


	function settime() {
		if(countdown == 0) {
			countdown = 60;
			$("#timecount").text(60);
			return;
		} else {
			$("#timecount").text(countdown);
			countdown--;
		}
		setTimeout(function() {
			settime()
		}, 1000)
	}

});

function removejscssfile(filename, filetype) {
	var targetelement = (filetype == "js") ? "script" : (filetype == "css") ? "link" : "none"
	var targetattr = (filetype == "js") ? "src" : (filetype == "css") ? "href" : "none"
	var allsuspects = document.getElementsByTagName(targetelement)
	for(var i = allsuspects.length; i >= 0; i--) {
		if(allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
			allsuspects[i].parentNode.removeChild(allsuspects[i])
	}
}